package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsAdmin;
import com.itqf.dto.DtsSearchDto;
import com.itqf.service.DtsAdminService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AdminResponseCode;
import com.itqf.utils.AdminResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/15 9:48
 * description:管理员模块
 */


@RestController
@CrossOrigin("*")
@RequestMapping("/admin/admin")
public class AdminAdminController {
    @Autowired
    private DtsAdminService dtsAdminService;
    public Logger logger = LoggerFactory.getLogger(AdminAdminController.class);
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "列表展示")
    @GetMapping("list")
    public Object list(DtsSearchDto dto){
        logger.debug("[管理员列表]管理员列表查询参数:{}",dto);
        Map data = dtsAdminService.findAdminByPage(dto);
        logger.debug("[管理员列表]分页查询数据为",data);
        return ResponseUtil.ok(data);
    }





    //管理员添加/admin/admin/create
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "管理员添加")
    @PostMapping("create")
    public Object create(@RequestBody DtsAdmin admin){
    //1·用户名是否在数据库存在的判断
        String username = admin.getUsername();
        if(username == null || username == ""){
            logger.warn("[保存管理员]接收到的用户名为null,无法注册!");
            return ResponseUtil.fail();
        }
        boolean hasUserName = dtsAdminService.checkUserName(username);
        if (hasUserName) {
            logger.error("[保存管理员]用户名:{},已经被注册:{}",username);
            return AdminResponseUtil.fail(AdminResponseCode.ADMIN_NAME_EXIST);
        }



        //2·保存用户
        Md5Hash md5Hash = new Md5Hash(admin.getPassword(),"store",2);
        admin.setPassword(md5Hash.toString());
        admin = dtsAdminService.saveAdmin(admin);
        //3·返回结果
        logger.info("[保存管理员]接口调用结束,获得回显的数据为:{}",admin);
        return ResponseUtil.ok(admin);
    }







    //管理员更新/admin/admin/update
    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "管理员修改")
    @PostMapping("update")
    public Object update(@RequestBody DtsAdmin dtsAdmin){
        //1·完善数据
        dtsAdmin.setPassword(new Md5Hash(dtsAdmin.getPassword(),"store",2).toString());
        //2·修改数据
        int rows = dtsAdminService.updateAdmin(dtsAdmin);
        //3·返回结果
        if (rows == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(dtsAdmin);
    }





    //管理员删除/admin/admin/delete
    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "管理员删除")
    @PostMapping("delete")
    public Object delete(@RequestBody DtsAdmin dtsAdmin){
        logger.debug("[删除管理员]传入的id为:{}",dtsAdmin.getId());
        int rows = dtsAdminService.removeAdminById(dtsAdmin.getId());
        if(rows == 0){
            return ResponseUtil.fail();
        }
        logger.info("[删除管理员]删除影响行数为",rows);
        return ResponseUtil.ok();
    }
}
